
import LogUtils from "@/CTManage/utils/LogUtils";
import ArrayUtils from "@/CTManage/utils/ArrayUtils";

const amountRegex = /^\d+(\.\d+)?$/;

const Tools = {

  name: 'Tools',

  /* 是否未定义或为null */
  isNull(obj) {
    return obj === undefined || obj === null;
  },

  /*
    判断是否为空
  */
  isEmpty(obj) {
    return Tools.isNull(obj) || obj === '';
  },

  isArrayEmpty(arr) { /* 判断数组是否为空 */
    return ArrayUtils.isEmpty(arr);
  },

  /*
    获取参数值
  */
  getValue(obj, key, debugMode) {

    if(debugMode) {
      console.log('---start---');
    }

    if(this.isEmpty(obj)
      || this.isEmpty(key)
    ) {
      /* console.log('对象或者取值字符串为空'); */
      return null;
    }

    let keys = key.split('.');

    let name = obj.constructor.name; // 类型名称
    let value = obj;
    for (let index = 0; index < keys.length; index++) {
      name += '.' + keys[index];
      value = value[keys[index]];

      if(debugMode) {
        console.log(name + '= ' + value);
      }

      if(Tools.isNull(value)) {
        if(debugMode) {
          console.log(name + ' 为空');
        }

        return null;
      }
    }

    if(debugMode) {
      console.log('---end---');
    }

    return value;
  },

  /*
    设置值
  */
  setValue(obj, key, value) {

    if(this.isNull(obj)
      || this.isNull(key)
    ) {
      console.log('对象或者取值字符串为空');
      return false;
    }

    if(key instanceof Array) {

      let notArr = false;
      let valueLen = 0;
      if(value) {
        if(value instanceof Array) {

          valueLen = value.length;
        } else {

          notArr = true;
        }
      }

      for(let i = 0; i < key.length; i++) {

        let val = null;
        if(notArr) {

          val = value;
        } else {
  
          if(valueLen > i) {
            val = value[i];
          }
        }

        this.setValue(obj, key[i], val);
      }
      return;
    }

    let keys = key.split('.');

    let name = obj.constructor.name; // 类型名称
    let attribute = obj;
    for (let index = 0; index < keys.length-1; index++) {

      let attribute2 = attribute;
      attribute = attribute[keys[index]];

      if(!attribute) {

        attribute = {};
        attribute2[keys[index]] = attribute;
      }
    }

    attribute[keys[keys.length-1]] = value;

    return true;
  },

  /*
    执行方法
   */
  execute(obj, str, ...params){
    let methodName = 'execute: ';
    let debug = false;

    if(debug) {
      LogUtils.debug(str, methodName + 'method');
      LogUtils.debug(params, methodName + 'params');
    }

    if(this.isEmpty(obj)) {
      if(debug) {
        LogUtils.debug(methodName + 'obj 为空');
      }
      return;
    }
    let method = this.getValue(obj, str);

    if(this.isEmpty(method)) {
      if(debug) {
        LogUtils.debug(methodName + 'method 为空');
      }
      return;
    }

    return method(...params);
  },

  /* 填充字符串，长度负数代表填充左边 */
  strFill(str, ch, length) {
    let methodName = 'strFill: ';

    /* LogUtils.debug(methodName + '原= ' + str); */

    if(str == null
        || ch === null || ch === ''
        || len == 0
      ) {
      return '';
    }

    /* LogUtils.debug(methodName + 'ch= ' + ch);
    LogUtils.debug(methodName + 'length= ' + length); */

    str = String(str);
    ch = String(ch);

    let left = length < 0;
    if(left) { // 负数转正
      length *= -1;
    }
    let len = length;

    len -= str.length; // 减去字符串本身长度
    len /= ch.length; // 计算拼接次数

    if(len <= 0) {
      /* LogUtils.debug(methodName + 'len小于等于0= ' + len); */
      return str;
    }

    if(left) { // 存在缺少 (1, ch.length - 1) 个字符的情况
      while(len-- > 0) {
        str = ch + str;
      }
    } else {
      while(len-- > 0) {
        str += ch;
      }
    }

    /* LogUtils.debug(methodName + '新= ' + str); */

    return str;
  },

  /* 数组是否存在属性值 */
  isArrExistKey(arr, val) {
    if(this.isEmpty(arr)
      || this.isNull(val)
      || !(arr instanceof Array)
    ) {
      return false;
    }

    for(let i = 0; i < arr.length; i++) {
      if(arr[i] == val) {
        return true;
      }
    }

    return false;
  },

  /* 对象是否存在属性 */
  isExistKey(obj, key) {
    let methodName = this.name + '.isExistKey: ';
    let debug = true;

    if(debug) {
      LogUtils.log(obj, methodName + 'obj');
      LogUtils.log(key, methodName + 'key');
    }

    let val = this.getValue(obj, key);

    if(debug) {
      LogUtils.log(val, methodName + 'val');
    }
    return !this.isNull(val);
  },

  /* 是否存在值 */
  isExistValue(obj, value, key) {
    let methodName = this.name + '.isExistValue: ';
    let debug = false;

    if(this.isNull(obj)
      || this.isNull(value)
    ) {
      if(debug) {
        LogUtils.log(methodName + 'obj或value为空');
      }
      return false;
    }

    if(debug) {
      LogUtils.log(obj, methodName + 'obj');
      LogUtils.log(key, methodName + 'key');
      LogUtils.log(value, methodName + 'value');
    }

    if(!Tools.isNull(key)) {
      return value == this.getValue(obj, key);
    }

    for(name in obj) {
      let val = this.getValue(obj, name);
      if(debug) {
        LogUtils.log(methodName + name + '= ' + val);
      }
      if(val == value) {
        return true;
      }
    }

    return false;
  },

  /* 值转换 */
  keyConvent(_this, val, formatter) {
    if(this.isNull(val)) {
      return '';
    }

    if(this.isNull(_this)
      || this.isNull(formatter)
    ) {
      return val;
    }

    return this.execute(_this, formatter, val);
  },

  /* 
    获取对象属性及值
    obj: 对象
    keys: 是否需要属性名称或者属性顺序
    convent: 属性值转换
    [{
      _this: this,
      prop: '属性名',
      formatter: '属性值转换方法，必须在 _this 中'
    }]
  */
  getObjValueArr(obj, keys, convent) {
    let methodName = 'getObjValueArr: ';
    let debug = false;

    if(this.isNull(obj)) {
      return null;
    }

    let arr = [];

    if(!(obj instanceof Object)) {
      if(debug) {
        LogUtils.log(methodName + 'obj 非对象');
      }
      return null;
    }

    if(debug) {
      LogUtils.log(convent, methodName + 'convent');
    }

    if(keys instanceof Array && keys.length > 0) {
      keys.forEach(e => {

        let val = this.getValue(obj, e);
        let conventObj = ArrayUtils.getObjHaveValueInObjArr(convent, e, 'prop');
        if(debug) {
          LogUtils.log(conventObj, methodName + 'conventObj');
        }

        if(!this.isNull(conventObj)) {
          val = Tools.execute(
            this.getValue(conventObj, '_this'),
            this.getValue(conventObj, 'formatter'),
            val
          );
        }
        arr.push(val);
      });
    } else if(keys === true) {
      for(name in obj) {

        let tmpArr = [];
        tmpArr.push(name);

        let val = this.getValue(obj, name);
        let conventObj = this.getObjHaveKeyInObjArr(convent, e);
        if(!this.isNull(conventObj)) {
          val = Tools.execute(
            this.getValue(conventObj, '_this'),
            this.getValue(conventObj, 'formatter'),
            val
          );
        }
        tmpArr.push(val);

        arr.push(tmpArr);
      }
    } else {
      for(name in obj) {
        arr.push(this.getValue(obj, name));
      }
    }

    return arr;
  },

  isAmount(amount) { // 验证是否金额类型 
    if(Tools.isEmpty(amount)) {
      return false;
    }

    return amountRegex.test(String(amount));
  },

  fillInteger(obj, key) { // 输入框中的值转为整数
    let val = this.getValue(obj, key);
    if(this.isEmpty(val)) {
      return;
    }

    let num = parseInt(String(val).split('.')[0]);

    if(isNaN(num)) {
      this.setValue(obj, key, '');
      return;
    }

    this.setValue(obj, key, String(num));
  }
}

export default Tools;
